package cn.pugle.base.conf;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;
import org.yaml.snakeyaml.error.YAMLException;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;

/**
 * 读取Yaml格式的配置文件，并映射到Config类上
 */
public class YamlConfigUtil {
    private static Logger logger = LoggerFactory.getLogger(YamlConfigUtil.class);

    /**
     * 加载配置文件，读取成Config类
     *
     * @param path        配置文件的路径
     * @param configClass 配置的类
     * @return
     */
    public static <E> E load(String path, Class<E> configClass) {
        try {
            Yaml y = new Yaml(new Constructor(configClass));
            E cfg = (E) y.load(path);
            return cfg;
        } catch (YAMLException e) {
            e.printStackTrace();
            logger.error("Yaml配置文件格式错误！[" + path + "]: ", e);
            return null;
        }
    }

    public static void main(String[] args) {
        try {
            ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
            ClassLoader classLoader = YamlConfigUtil.class.getClassLoader();
            System.out.println("Sys CL : " + systemClassLoader.getClass().getSimpleName());
            System.out.println("Ths CL : " + classLoader.getClass().getSimpleName());
            if (systemClassLoader.equals(classLoader)) {
                System.out.println("Same CL");
            }
            try {
                System.out.println(classLoader.getResource(".").toURI());
            } catch (NullPointerException e) {
                System.out.println("Ex: YamlConfigUtil.class.getClassLoader().getResource(\".\").toURI()");
            }
            try {
                System.out.println(systemClassLoader.getResource(".").toURI());
            } catch (NullPointerException e) {
                System.out.println("Ex: ClassLoader.getSystemClassLoader().getResource(\".\").toURI()");
            }
            try {
                System.out.println(ClassLoader.getSystemResource(".").toURI());
            } catch (NullPointerException e) {
                System.out.println("Ex: ClassLoader.getSystemResource(\".\").toURI()");
            }
            try {
                System.out.println(YamlConfigUtil.class.getResource("/x").toURI());// 看这个方法的文档注释！！！＠see http://stackoverflow.com/questions/6608795/what-is-the-difference-between-class-getresource-and-classloader-getresource
            } catch (NullPointerException e) {
                System.out.println("Ex: YamlConfigUtil.class.getResource(\".\").toURI()");
            }

            //Spring(其实就是用class和classloader分别getResource)
            ClassPathResource r = new ClassPathResource("config.yml");//xxx
            System.out.println(r.getURI());


            /** result
             Same CL
             file:/C:/Users/tzp/Projects/CodeAccumulate/target/classes/
             file:/C:/Users/tzp/Projects/CodeAccumulate/target/classes/
             file:/C:/Users/tzp/Projects/CodeAccumulate/target/classes/
             file:/C:/Users/tzp/Projects/CodeAccumulate/target/classes/cn/pugle/base/Config/
             file:/C:/Users/tzp/Projects/CodeAccumulate/target/classes/config.yml
             * */

            //            InputStream input = new FileInputStream(new File("src/main/resources/config.yml"));
            InputStream resourceAsStream = classLoader.getResourceAsStream("config.yml");
            System.out.println(resourceAsStream.available());

            Yaml y = new Yaml(new Constructor(Config.class));
            Config config = (Config) y.load(resourceAsStream);
            System.out.println(config.HDFS_URI);


            //检测某个类从哪里加载的方法
//        ClassLoader classLoader = EntryClass.class.getClassLoader();
//        URL resource = classLoader.getResource("org/apache/http/conn/ssl/AllowAllHostnameVerifier.class");
//        System.out.println("----hehe----"+ resource);

//        由于tomcat环境下classpath诡异，建议使用Spirng的ClassPathResource加载资源
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    }
}
